package cn.handong.tool.task;

import cn.handong.tool.pojo.Stock;
import cn.handong.tool.pojo.StockDetail;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;




public class StockTask {

    public static void main(String[] args) throws IOException {

    }

    @Test
    public void getStockInfo() throws Exception{

        String code = "002594";
        String baseUrl = "https://push2.eastmoney.com/api/qt/stock/get?fltt=2&invt=2&fields=f78,f58,f59,f86,f43,f169,f170,f44,f45,f46,f47,f116,f117,f162,f167,f60,f137,f469,f434,f470,f459,f471,f292&ut=b2884a393a59ad64002292a3e90d46a5&_=1650340962936&secid=0.";
        URL url = new URL(baseUrl+code);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod("GET");

        httpConn.setRequestProperty("Accept", "*/*");
        httpConn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
        httpConn.setRequestProperty("Connection", "keep-alive");
        httpConn.setRequestProperty("Cookie", "qgqp_b_id=2e099ea836a522411afb5ecc1aec123e; cowCookie=true; st_si=02954074293221; intellpositionL=1215.35px; cowminicookie=true; st_asi=delete; st_pvi=83724081056603; st_sp=2022-04-18%2022%3A02%3A08; st_inirUrl=http%3A%2F%2Fquote.eastmoney.com%2F; st_sn=4; st_psi=20220419120226769-113300301471-1899912758; intellpositionT=755px");
        httpConn.setRequestProperty("Referer", "https://data.eastmoney.com/");
        httpConn.setRequestProperty("Sec-Fetch-Dest", "script");
        httpConn.setRequestProperty("Sec-Fetch-Mode", "no-cors");
        httpConn.setRequestProperty("Sec-Fetch-Site", "same-site");
        httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36");
        httpConn.setRequestProperty("sec-ch-ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Google Chrome\";v=\"100\"");
        httpConn.setRequestProperty("sec-ch-ua-mobile", "?0");
        httpConn.setRequestProperty("sec-ch-ua-platform", "\"Windows\"");

        InputStream responseStream = httpConn.getResponseCode() / 100 == 2
                ? httpConn.getInputStream()
                : httpConn.getErrorStream();
        Scanner s = new Scanner(responseStream).useDelimiter("\\A");
        String response = s.hasNext() ? s.next() : "";
        JSONObject jsonObject = JSONObject.parseObject(response);
        JSONObject data = jsonObject.getJSONObject("data");

        StockDetail stockDetail = new StockDetail();

        stockDetail.setCode(code);
        stockDetail.setName(data.getString("f58"));
        stockDetail.setLatestPrice(data.getDouble("f43"));
        stockDetail.setMaxPrice(data.getDouble("f44"));
        stockDetail.setMinPrice(data.getDouble("f45"));
        stockDetail.setOpeningPrice(data.getDouble("f46"));
        stockDetail.setIncreaseAmount(data.getDouble("f170"));
        stockDetail.setTurnOver(data.getLong("f47"));
        stockDetail.setTotalMarketValue(data.getLong("f116"));
        stockDetail.setMainNetInflow(data.getLong("f137"));
        System.out.println(response);
    }

    @Test
    public void justTest(){
        System.out.println(System.currentTimeMillis());
    }



    @Test
    public void getStockFundFlow() throws Exception {
        String str = "https://push2.eastmoney.com/api/qt/clist/get?fid=f62&pz=50&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A13%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A7%2Bf%3A!2%2Cm%3A1%2Bt%3A3%2Bf%3A!2&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13&po=1&pn=";
        JSONArray js;
        List<Stock> stockList = new ArrayList<>();
        for (int i = 1; i <= 97; i++) {
            System.out.println(i);
            js = getStockFlowData(str + i);
            for (Iterator<Object> iterator = js.iterator(); iterator.hasNext(); ) {
                JSONObject next = (JSONObject) iterator.next();
                if (!(next.getString("f2").equals("-"))){
                    Stock stock = new Stock();
                    stock.setName(next.getString("f14"));
                    stock.setCode(next.getString("f12"));
                    stock.setIncreaseAmount(next.getDouble("f3"));
                    stock.setLatestPrice(next.getDouble("f2"));
                    stock.setMainNetInflow(next.getLong("f62"));
                    if (stock.getLatestPrice() < 20 && stock.getMainNetInflow() > 20000000 && stock.getIncreaseAmount() < 0){
                        stockList.add(stock);
                    }
                }
            }
        }
        stockList.sort((y,x)->Double.compare(x.getLatestPrice(),y.getLatestPrice()));

        String filename = "D:\\StockData\\stock-2022-4-19.xlsx";
        // 2 调用easyexcel里面的方法实现写操作
        // write方法两个参数：第一个参数文件路径名称，第二个参数实体类class
        EasyExcel.write(filename,Stock.class).sheet("股票列表").doWrite(stockList);
    }

    public JSONArray getStockFlowData(String str) throws Exception{
        URL url = new URL(str);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod("GET");
        httpConn.setRequestProperty("Accept", "*/*");
        httpConn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
        httpConn.setRequestProperty("Connection", "keep-alive");
        httpConn.setRequestProperty("Cookie", "qgqp_b_id=2e099ea836a522411afb5ecc1aec123e; cowCookie=true; st_si=02954074293221; intellpositionL=1215.35px; st_pvi=83724081056603; st_sp=2022-04-18%2022%3A02%3A08; st_inirUrl=http%3A%2F%2Fquote.eastmoney.com%2F; st_sn=2; st_psi=20220419105848694-113300300813-7073635291; cowminicookie=true; st_asi=20220419105848694-113300300813-7073635291-dfcfwsy_dfcfwxsy_dcxn_djgb-1; intellpositionT=904.6px");
        httpConn.setRequestProperty("Referer", "https://data.eastmoney.com/");
        httpConn.setRequestProperty("Sec-Fetch-Dest", "script");
        httpConn.setRequestProperty("Sec-Fetch-Mode", "no-cors");
        httpConn.setRequestProperty("Sec-Fetch-Site", "same-site");
        httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36");
        httpConn.setRequestProperty("sec-ch-ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"100\", \"Google Chrome\";v=\"100\"");
        httpConn.setRequestProperty("sec-ch-ua-mobile", "?0");
        httpConn.setRequestProperty("sec-ch-ua-platform", "\"Windows\"");
        InputStream responseStream = httpConn.getResponseCode() / 100 == 2
                ? httpConn.getInputStream()
                : httpConn.getErrorStream();
        Scanner s = new Scanner(responseStream).useDelimiter("\\A");
        String response = s.hasNext() ? s.next() : "";
        JSONObject parse = JSONObject.parseObject(response);
        JSONObject data = parse.getJSONObject("data");
        JSONArray diff = data.getJSONArray("diff");
        return diff;
    }




}